Imports BikiSoft.DataStream
Imports System.Reflection
Imports System.Runtime.InteropServices

Namespace Innovate

    Public Class Innovate_LC_1
        Implements IStreamRuntime, IStreamRecieve

        Private WithEvents m_MTS As New MTSSDKLib.MTS
        Private m_5v As Double = 0
        Private m_lambda As Double = 0
        Private m_afr As Double = 0

#Region " MTS Events"
        Private Sub m_MTS_ConnectionError() Handles m_MTS.ConnectionError
            m_MTS.Disconnect()
            eState = _eReadStates.eReadUnSynced
        End Sub

        Private Sub m_MTS_ConnectionEvent(ByVal Result As Integer) Handles m_MTS.ConnectionEvent
            If Result = 0 Then
                eState = _eReadStates.eReadSync
                m_MTS.StartData()
            Else
                eState = _eReadStates.eReadFind
            End If
        End Sub

        Private Sub m_MTS_NewData() Handles m_MTS.NewData
            For i As Integer = 0 To m_MTS.InputCount - 1
                m_MTS.CurrentInput = i
                Select Case m_MTS.InputType
                    Case 1
                        If m_MTS.InputFunction = 0 Then
                            m_afr = ((m_MTS.InputSample / 1024) + 0.5) * m_MTS.InputAFRMultiplier
                        End If
                    Case 2
                        If m_MTS.InputFunction = 9 Then
                            m_5v = ((m_MTS.InputSample * (m_MTS.InputMaxValue - m_MTS.InputMinValue)) _
                                / 1024) + m_MTS.InputMinValue
                        End If
                    Case 3

                End Select
            Next

        End Sub
#End Region

        Public Event Close(ByVal sender As Object, ByVal e As System.EventArgs) Implements BikiSoft.DataStream.IStreamRecieve.Close

        Public ReadOnly Property RecieverType() As BikiSoft.DataStream.StreamRecieveType Implements BikiSoft.DataStream.IStreamRecieve.RecieverType
            Get


            End Get
        End Property

        Public Sub RecieveSeek(ByVal Stream As BikiSoft.DataStream.Stream) Implements BikiSoft.DataStream.IStreamRecieve.RecieveSeek

        End Sub

        Public Sub RecieveStream(ByVal Stream As BikiSoft.DataStream.Stream) Implements BikiSoft.DataStream.IStreamRecieve.RecieveStream

        End Sub

        Public Event RequestRefresh(ByVal sender As Object, ByVal e As System.EventArgs) Implements BikiSoft.DataStream.IStreamRecieve.RequestRefresh

        Public Event Restrict(ByVal sender As Object, ByVal e As BikiSoft.DataStream.StreamRestrictEventArgs) Implements BikiSoft.DataStream.IStreamRecieve.Restrict

        Public Event Seek(ByVal sender As Object, ByVal e As BikiSoft.DataStream.StreamSeekEventArgs) Implements BikiSoft.DataStream.IStreamRecieve.Seek

        Public Event AddControl(ByVal sender As Object, ByVal Control As Object) Implements BikiSoft.DataStream.IStreamRuntime.AddControl

        Public ReadOnly Property Description() As String Implements BikiSoft.DataStream.IStreamRuntime.Description
            Get
                Return "Innovate LC1 Wideband Serial Interface Bikirom Extension. Automaticaly connects to any available Innovate LC1 device"
            End Get
        End Property

        Public Event DisplayMessage(ByVal sender As Object, ByVal e As BikiSoft.DataStream.DisplayMessageEventArgs) Implements BikiSoft.DataStream.IStreamRuntime.DisplayMessage

        Private m_ThreadMaster As Threading.Thread = Nothing
        Private WithEvents m_TimeOut As New Timers.Timer(1000)

        Public Function Initialise() As Boolean Implements BikiSoft.DataStream.IStreamRuntime.Initialise
            m_ThreadMaster = New Threading.Thread(AddressOf Execute)
            m_ThreadMaster.Start()
        End Function

        Private Enum _eReadStates As Byte
            eReadFind
            eReadUnSynced
            eReadSync
        End Enum

        Dim eState As _eReadStates = _eReadStates.eReadFind

        Public Sub Execute(ByVal obj As Object)
            Dim bSync As Boolean = False
            Dim nPortNum As UInt16 = 0

            Try

                While True
                    Threading.Thread.Sleep(1000)
                    mSerialMutex.WaitOne()

                    Select Case eState

                        Case _eReadStates.eReadFind

                            Try
                                If nPortNum >= My.Computer.Ports.SerialPortNames.Count Then
                                    nPortNum = 1
                                Else
                                    nPortNum += 1
                                End If

                                Me.m_MTS.CurrentPort = nPortNum

                                Me.m_MTS.Connect()
                            Catch ex As UnauthorizedAccessException
                                Threading.Thread.Sleep(1000)
                                m_TimeOut.Stop()
                            Catch ex As IO.IOException
                                Threading.Thread.Sleep(1000)
                                m_TimeOut.Stop()
                            End Try

                        Case eState = _eReadStates.eReadUnSynced
                            Me.m_MTS.Connect()
                    End Select

                    mSerialMutex.ReleaseMutex()

                End While
            Catch ex As Threading.ThreadAbortException
                RaiseEvent DisplayMessage(Me, New BikiSoft.DataStream.DisplayMessageEventArgs("TechEdge: xtension has been aborted"))
            Finally
                mSerialMutex.ReleaseMutex()
                Me.m_MTS.Disconnect()
            End Try

        End Sub

        Public ReadOnly Property Logo() As System.Drawing.Icon Implements BikiSoft.DataStream.IStreamRuntime.Logo
            Get
                Return Nothing
            End Get
        End Property

        Public ReadOnly Property MenuItems() As System.Windows.Forms.ToolStripMenuItem() Implements BikiSoft.DataStream.IStreamRuntime.MenuItems
            Get
                Return Nothing
            End Get
        End Property

        Public Event RemoveControl(ByVal sender As Object, ByVal Control As Object) Implements BikiSoft.DataStream.IStreamRuntime.RemoveControl

        Public Sub ShowDialogForm() Implements BikiSoft.DataStream.IStreamRuntime.ShowDialogForm

        End Sub

        Public Event UnhandledException(ByVal sender As Object, ByVal e As System.EventArgs) Implements BikiSoft.DataStream.IStreamRuntime.UnhandledException

        Public ReadOnly Property Version() As String Implements BikiSoft.DataStream.IStreamRuntime.Version
            Get
                Return "V1.0.0"
            End Get
        End Property

        Public ReadOnly Property Name() As String Implements BikiSoft.INamed.Name
            Get
                Return "Innovate-LC1 Wideband"
            End Get
        End Property

        Private disposedValue As Boolean = False        ' To detect redundant calls

        ' IDisposable
        Protected Overridable Sub Dispose(ByVal disposing As Boolean)
            If Not Me.disposedValue Then
                If disposing Then
                    ' TODO: free managed resources when explicitly called
                End If

                ' TODO: free shared unmanaged resources
            End If
            Me.disposedValue = True
        End Sub

#Region " IDisposable Support "
        ' This code added by Visual Basic to correctly implement the disposable pattern.
        Public Sub Dispose() Implements IDisposable.Dispose
            ' Do not change this code.  Put cleanup code in Dispose(ByVal disposing As Boolean) above.
            Dispose(True)
            GC.SuppressFinalize(Me)
        End Sub
#End Region

    End Class

End Namespace
